﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using CMISLibrary.Libraries;
using BaocaoNPC.Usercontrols.reports;
using BaocaoNPC.common;
using CMISLibrary;
using BCNPCEntities;
using System.Threading;

namespace CMIS2.BaocaoNPC.Usercontrols
{
    /// <summary>
    /// Interaction logic for NPC_BCKD05.xaml
    /// </summary>
    public partial class NPC_BCKD05 : UserControl
    {
        #region KhaiBaoBien
        /*
        private string strMa_dviqly = UserInfomation.MaDViqly != "" && UserInfomation.MaDViqly != null ? UserInfomation.MaDViqly : "PA25";
        private string strTenDNHap = UserInfomation.TenDNHap != "" && UserInfomation.TenDNHap != null ? UserInfomation.TenDNHap : "PD0200";
        private string strCurrentLibID = MenuInfomation.LibID != "" && MenuInfomation.LibID != null ? MenuInfomation.LibID : "65";
        private string strTen_dviqly = UserInfomation.TenDViQLy != "" && UserInfomation.TenDViQLy != null ? UserInfomation.TenDViQLy : "Dien luc Vinh Phuc";
        private string strTen_dviqly_CT = UserInfomation.TenDViQLy_CT != "" && UserInfomation.TenDViQLy_CT != null ? UserInfomation.TenDViQLy_CT : "Cong ty Dien luc Vinh Phuc";
         * */

        public String strMa_dviqly = NPCUS_DanhMuc.Pglobal.strMa_dviqly != "" && NPCUS_DanhMuc.Pglobal.strMa_dviqly != null ? NPCUS_DanhMuc.Pglobal.strMa_dviqly : "PA";
        public String strTenDNHap = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "PA";
        public String strTen_dviqly = NPCUS_DanhMuc.Pglobal.strTen_dviqly != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly : "cty..";
        public String strTen_dviqly_CT = NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT : "cap tren";

        string strError = "";
        string strThemSuaXoa = "";
        string strTim = "";//"CoDuLieu"; "KhongCoDuLieu"
        string strThongBao = "";
        Int16 p_thang,ifthang;
        Int16 p_nam,ifnam;
        Int16 intDongTrenGrid;
        DataSet ds;
        DataSet dsbieu05_Entity;
        DataSet dsbieu17_Entity;
        ListItem lstItem;
        DataTable dtHienThi = new DataTable();
        DataTable dtbieu17 = new DataTable();
        DataSet dshuynhap;
        #endregion
        
        //============================================================================================================================
        #region KhoiTaoThongTinKhiLoadForm

        private void setEntity(string str)
        {
            try
            {
                switch (str)
                    {
                        case "1": //Loadcombo
                            dsbieu05_Entity.Tables["D_DVI_QLY"].Rows.Clear();
                            DataRow dr = dsbieu05_Entity.Tables["D_DVI_QLY"].NewRow();
                            dr["TEN_DVIQLY"] = cboten_donvi.Text.Trim();
                                    //break;
                            dsbieu05_Entity.Tables["D_DVI_QLY"].Rows.Add(dr);
                            dsbieu05_Entity.AcceptChanges();
                            break;
                        case "2": //Loadgrid
                            dsbieu17_Entity.Tables["PC1_BCKD17"].Rows.Clear();
                            DataRow dr1 = dsbieu17_Entity.Tables["PC1_BCKD17"].NewRow();
                            //dr1["DOI_TUONG"] = " ";
                            //dr1["TS_KH"] = "";
                            //break;
                            dsbieu17_Entity.Tables["PC1_BCKD17"].Rows.Add(dr1);
                            dsbieu17_Entity.AcceptChanges();
                            break;
                    }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void InitCombo_cbo()
        {
            setEntity("1");
            //PC1Business.cls_D_DVI_QLY cls = new PC1Business.cls_D_DVI_QLY();
            DataSet dscobo =  GlobalServices.Service_NPCUS.select_D_DVI_QLY_ALL(strMa_dviqly);
            dtHienThi = dscobo.Tables[0].Copy();
            string strdonvi = "";
            if (dtHienThi.Rows.Count > 0)
            {
                foreach (DataRow row in dtHienThi.Rows)
                {
                    lstItem = new ListItem();
                    strdonvi = Convert.ToString(row["TEN_DVIQLY"]);
                    //lstItem.v = Convert.ToString(row["TEN_DVIQLY"]);
                    cboten_donvi.Items.Add(strdonvi);
                    cboten_donvi.SelectedIndex =0 ;
                }
            }
        }
        private void Initgrid(string ma_donvi)
        {
            setEntity("2");
            if (ma_donvi.ToString().Length == 4)
            {
                DataSet dscobocty = GlobalServices.Service_NPCUS.Chon_cty_dienluc(p_nam, p_thang,ma_donvi, 17);
            }
            DataSet dscobo = GlobalServices.Service_NPCUS.select_PC1_BCKD17(ma_donvi, p_thang, p_nam);
                dshuynhap = dscobo.Copy();
                dtbieu17 = dscobo.Tables[0].Copy();
            //DataSet dscobo = GlobalServices.Service_NPC.select_donviqly(dsD_BCKD_Entity);
            if (dtbieu17.Rows.Count > 0)
            {
                //DataTable dtTemp = dsD_BCKD_Entity.Tables["PC1_D_BCKD"].Copy();                            
                //dtHienThi = dtTemp;
                //strTim = "CoDuLieu";
                dgrNPC_BCKD05.ItemsSource = dtbieu17.DefaultView;
               // CheckIsEnableButtonSuaXoa();
                dgrNPC_BCKD05.SelectedIndex = 0; ;
            }
        }
        #endregion
        
        //============================================================================================================================

        //============================================================================================================================
        public NPC_BCKD05()
        {
            try
            {

                InitializeComponent();
                dsbieu05_Entity = new BCNPCEntities.CMIS2.D_DVI_QLY();
                dtHienThi = dsbieu05_Entity.Tables["D_DVI_QLY"].Clone();
                dsbieu17_Entity = new BCNPCEntities.BCKD15.PC1_D_BCKD_Entity();
                dtbieu17 = dsbieu17_Entity.Tables["PC1_BCKD17"].Clone();
                GetThangNamBC();

                InitCombo_cbo();
                khoitao_bieu17();
                dgrNPC_BCKD05.IsReadOnly=true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            cmdhuy.IsEnabled = false;
            cmdnhap.IsEnabled = false;
            cmdghi.IsEnabled = false;
            GetThangNamBC();
        }
        private void GetThangNamBC()
        {
            try
            {
                GET_THANGNAM();

                if (p_thang.ToString().Length == 1)
                {
                    dtpThangNam.Text = "0" + p_thang.ToString() + "/" + p_nam.ToString();
                }
                else
                {
                    dtpThangNam.Text = p_thang.ToString() + "/" + p_nam.ToString();
                }

                //========      Xac dinh ngay cuoi thang de hien thi vao datetime picker    ========
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }
        private void cmdchon_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                for (int i = 0; i < dtHienThi.Rows.Count; i++)
                {
                    if (cboten_donvi.Text.ToString() == dtHienThi.Rows[i]["TEN_DVIQLY"].ToString())
                    {
                        strMa_dviqly = dtHienThi.Rows[i]["MA_DVIQLY"].ToString();
                    }
                }
                // MessageBox.Show(strMa_dviqly);
                khoitao_bieu17();
                p_thang = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Month);
                p_nam = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Year);

                Initgrid(strMa_dviqly);
                //if (strMa_dviqly.ToString().Length == 4)
                //    {
                //        cmdnhap.IsEnabled = false;
                //        cmdhuy.IsEnabled = false;
                //        cmdghi.IsEnabled = false;
                //    }
                //    else
                //    {
                if (p_thang == ifthang && p_nam == ifnam)
                {
                    cmdnhap.IsEnabled = true;
                    cmdhuy.IsEnabled = false;
                    cmdghi.IsEnabled = false;
                }
                else
                {
                    cmdnhap.IsEnabled = false;
                    cmdhuy.IsEnabled = false;
                    cmdghi.IsEnabled = false;
                }
                ////}
                dgrNPC_BCKD05.IsReadOnly = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi : " + ex.ToString(), "Thông báo");
                strTim = "";
                return;
            }
        }
        private void cmdin_Click(object sender, RoutedEventArgs e)
        {
                try
                {
                    for (int i = 0; i < dtHienThi.Rows.Count; i++)
                    {
                        if (cboten_donvi.Text.ToString() == dtHienThi.Rows[i]["TEN_DVIQLY"].ToString())
                        {
                            strMa_dviqly = dtHienThi.Rows[i]["MA_DVIQLY"].ToString();
                        }
                    } 
                    //Hien thi con tro bao ban, khi bat dau nhan nut In

                    this.Cursor = Cursors.Wait;
                    Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new ThreadStart(delegate { }));
                    p_thang = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Month);
                    p_nam = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Year);
                    DataSet ds1 = new DataSet();
                    setEntity("2");
                    //PC1Business.cls_pc1_bieu17 cls = new PC1Business.cls_pc1_bieu17();
                    //DataSet dscobo = cls.select_PC1_BCKD17(strMa_dviqly,p_thang,p_nam);
                    DataSet dscobo = GlobalServices.Service_NPCUS.INBIEU(strMa_dviqly, p_nam, p_thang,17);
                    
                    //PC1Business.cls_PA_TH_IN_BC_SCHT cls = new PC1Business.cls_PA_TH_IN_BC_SCHT();
                    //ds1 = cls.THopBCao_SCHT(intNam_pkg, intThang_pkg, strMa_dviqly_pkg, strTuNgay_pkg, strDenNgay_pkg, strMa_KQua_pkg, iCase_pkg, ref strErr);
                    //ds_BCKD_QTTH = GlobalServices.Service_NPC.THopBCao_SCHT(intNam_pkg, intThang_pkg, strMa_dviqly_pkg, strTuNgay_pkg, strDenNgay_pkg, strMa_KQua_pkg, iCase_pkg, ref strErr);

                    //Goi report de hien thi du lieu tim duoc
                    rpt_PC1_BCKD17 crt = new rpt_PC1_BCKD17();
                    crt.SetDataSource(dscobo);
                    //UtilReport.RptManager.showReportToWin(this, crt, strMa_dviqly);
                    UtilReportOMS.RptManager.showReportToWin(this, crt, strMa_dviqly, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");

                    //Chuyen che do con tro ve trang thai ban dau
                    this.Cursor = null;
                }
                catch (Exception EX)
                {
                    MessageBox.Show(EX.ToString());
                }
            }

        private void cmdnhap_Click(object sender, RoutedEventArgs e)
        {
            cmdghi.IsEnabled = true;
            cmdhuy.IsEnabled = true;
            cmdin.IsEnabled = false;
            cmdchon.IsEnabled = false;
            cmdnhap.IsEnabled = false;
            dgrNPC_BCKD05.IsReadOnly = false;
            dgrNPC_BCKD05.Focus();
            dgrNPC_BCKD05.SelectedIndex = 1;
        }

        private void cmdhuy_Click(object sender, RoutedEventArgs e)
        {
            cmdchon.IsEnabled = true;
            cmdghi.IsEnabled = false;
            cmdin.IsEnabled = true;
            cmdnhap.IsEnabled = true;
            cmdhuy.IsEnabled = false;
            dgrNPC_BCKD05.IsReadOnly = true;
            dtbieu17 = dshuynhap.Tables[0].Copy();
            if (dtbieu17.Rows.Count > 0)
            {
                dgrNPC_BCKD05.ItemsSource = dtbieu17.DefaultView;
                dgrNPC_BCKD05.SelectedIndex = 1; ;
            }
        }
        private void khoitao_bieu17()
        {
            //PC1Business.cls_pc1_bieu17 cls = new PC1Business.cls_pc1_bieu17();
            p_thang = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Month);
            p_nam = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Year);
            ds = GlobalServices.Service_NPCUS.KHOITAO_BIEU17(p_nam, p_thang, strMa_dviqly);
            // MessageBox.Show(drv.Row["TS_KH"].ToString());
        }
        private void GET_THANGNAM()
        {
            try
            {
                DataSet dsthanglviec = new DataSet();
                //bus_TienichNPC.cls_TienichNPC_bus bus = new cls_TienichNPC_bus();
                //dsthanglviec = bus.get_thang_lviec();
                dsthanglviec = GlobalServices.Service_NPCUS.get_thang_lviec();
                if (dsthanglviec != null && dsthanglviec.Tables.Count > 0)
                {
                    foreach (DataRow dr in dsthanglviec.Tables[0].Rows)
                        if (dr["PHAN_HE"].ToString() == "BC" && dr["MA_DVIQLY"].ToString() == strMa_dviqly)
                        {
                            p_thang = Convert.ToInt16(dr["THANG"]);
                            p_nam = Convert.ToInt16(dr["NAM"]);
                            ifthang = Convert.ToInt16(dr["THANG"]);
                            ifnam = Convert.ToInt16(dr["NAM"]);
                        }
                }

            }
            catch
            {
                p_thang = Convert.ToInt16(DateTime.Now.Month); ;
                p_nam = Convert.ToInt16(DateTime.Now.Year); ;
                ifthang = Convert.ToInt16(DateTime.Now.Month);
                ifnam = Convert.ToInt16(DateTime.Now.Year);;
            }
        }
        private void cmdghi_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string ma_so;
                Int64 TS_KH, SL_BBKT, SL_VPHC, KQ_HH_CT, KQ_HH_TUTI, KQ_HH_MD, KQ_HH_HSN, KQ_HH_SK, KQ_VP_TCD_CM, KQ_VP_TCD_XPCT, KQ_VP_TCD_XPMD, KQ_VP_TCD_HTK, KQ_VP_CSGCD, KQ_VP_GD_SMD, KQ_VP_GD_AGS, KQ_VP_VPK;
                DataRowView drv = dgrNPC_BCKD05.SelectedItem as DataRowView;

                cmdhuy.IsEnabled = false;
                cmdin.IsEnabled = true;
                cmdnhap.IsEnabled = true;
                cmdghi.IsEnabled = false;
                cmdchon.IsEnabled = true;
                //PC1Business.cls_pc1_bieu17 cls = new PC1Business.cls_pc1_bieu17();
                p_thang = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Month);
                p_nam = Convert.ToInt16(dtpThangNam.SelectedDate.Value.Year);
                for (int i = 1; i < 6; i++)
                {
                    ma_so = dtbieu17.Rows[i]["MA_SO"].ToString().Trim();
                    if (dtbieu17.Rows[i]["TS_KH"].ToString().Trim() == "")
                    {
                        TS_KH = 0;
                    }
                    else
                    {
                        TS_KH = Convert.ToInt64(dtbieu17.Rows[i]["TS_KH"].ToString());
                    }
                    if (dtbieu17.Rows[i]["SL_BBKT"].ToString().Trim() == "")
                    {
                        SL_BBKT = 0;
                    }
                     else
                    {
                        SL_BBKT = Convert.ToInt64(dtbieu17.Rows[i]["SL_BBKT"].ToString());
                    }
                    if (dtbieu17.Rows[i]["SL_VPHC"].ToString().Trim() == "")
                    {
                        SL_VPHC = 0;
                    }
                     else
                    {
                        SL_VPHC = Convert.ToInt64(dtbieu17.Rows[i]["SL_VPHC"].ToString());
                    }
                    if (dtbieu17.Rows[i]["KQ_HH_CT"].ToString().Trim() == "")
                    {
                        KQ_HH_CT = 0;
                    }
                     else
                    {
                        KQ_HH_CT = Convert.ToInt64(dtbieu17.Rows[i]["KQ_HH_CT"].ToString());
                    }
                    if (dtbieu17.Rows[i]["KQ_HH_TUTI"].ToString().Trim() == "")
                    {
                        KQ_HH_TUTI = 0;
                    }
                     else
                    {
                        KQ_HH_TUTI = Convert.ToInt64(dtbieu17.Rows[i]["KQ_HH_TUTI"].ToString());
                    }
                    if (dtbieu17.Rows[i]["KQ_HH_MD"].ToString().Trim() == "")
                    {
                        KQ_HH_MD = 0;
                    }
                     else
                    {
                        KQ_HH_MD = Convert.ToInt64(dtbieu17.Rows[i]["KQ_HH_MD"].ToString());
                    }
                    if (dtbieu17.Rows[i]["KQ_HH_HSN"].ToString().Trim() == "")
                    {
                        KQ_HH_HSN = 0;
                    }
                     else
                    {
                        KQ_HH_HSN = Convert.ToInt64(dtbieu17.Rows[i]["KQ_HH_HSN"].ToString());
                    }
                    if (dtbieu17.Rows[i]["KQ_HH_SK"].ToString().Trim() == "")
                    {
                        KQ_HH_SK = 0;
                    }
                     else
                    {
                        KQ_HH_SK = Convert.ToInt64(dtbieu17.Rows[i]["KQ_HH_SK"].ToString());
                    }
                    if (dtbieu17.Rows[i]["KQ_VP_TCD_CM"].ToString().Trim() == "")
                    {
                        KQ_VP_TCD_CM = 0;
                    }
                     else
                    {
                        KQ_VP_TCD_CM = Convert.ToInt64(dtbieu17.Rows[i]["KQ_VP_TCD_CM"].ToString());
                    }
                    if (dtbieu17.Rows[i]["KQ_VP_TCD_XPCT"].ToString().Trim() == "")
                    {
                        KQ_VP_TCD_XPCT = 0;
                    }
                     else
                    {
                        KQ_VP_TCD_XPCT = Convert.ToInt64(dtbieu17.Rows[i]["KQ_VP_TCD_XPCT"].ToString());
                    }
                    if (dtbieu17.Rows[i]["KQ_VP_TCD_XPMD"].ToString().Trim() == "")
                    {
                        KQ_VP_TCD_XPMD = 0;
                    }
                     else
                    {
                        KQ_VP_TCD_XPMD = Convert.ToInt64(dtbieu17.Rows[i]["KQ_VP_TCD_XPMD"].ToString());
                    }
                    if (dtbieu17.Rows[i]["KQ_VP_TCD_HTK"].ToString().Trim() == "")
                    {
                        KQ_VP_TCD_HTK = 0;
                    }
                     else
                    {
                        KQ_VP_TCD_HTK = Convert.ToInt64(dtbieu17.Rows[i]["KQ_VP_TCD_HTK"].ToString());
                    }
                    if (dtbieu17.Rows[i]["KQ_VP_CSGCD"].ToString().Trim() == "")
                    {
                        KQ_VP_CSGCD = 0;
                    }
                     else
                    {
                        KQ_VP_CSGCD = Convert.ToInt64(dtbieu17.Rows[i]["KQ_VP_CSGCD"].ToString());
                    }
                    if (dtbieu17.Rows[i]["KQ_VP_GD_SMD"].ToString().Trim() == "")
                    {
                        KQ_VP_GD_SMD = 0;
                    }
                     else
                    {
                        KQ_VP_GD_SMD = Convert.ToInt64(dtbieu17.Rows[i]["KQ_VP_GD_SMD"].ToString());
                    }
                    if (dtbieu17.Rows[i]["KQ_VP_GD_AGS"].ToString().Trim() == "")
                    {
                        KQ_VP_GD_AGS = 0;
                    }
                     else
                    {
                        KQ_VP_GD_AGS = Convert.ToInt64(dtbieu17.Rows[i]["KQ_VP_GD_AGS"].ToString());
                    }
                    if (dtbieu17.Rows[i]["KQ_VP_VPK"].ToString().Trim() == "")
                    {
                        KQ_VP_VPK = 0;
                    }
                     else
                    {
                        KQ_VP_VPK = Convert.ToInt64(dtbieu17.Rows[i]["KQ_VP_VPK"].ToString());
                    }
                    //bus_TienichNPC.cls_TienichNPC_bus bus = new bus_TienichNPC.cls_TienichNPC_bus();

                    //ds = bus.UPDATE_BIEU17(p_nam, p_thang, strMa_dviqly, ma_so, TS_KH, SL_BBKT, SL_VPHC, KQ_HH_CT, KQ_HH_TUTI, KQ_HH_MD, KQ_HH_HSN, KQ_HH_SK, KQ_VP_TCD_CM, KQ_VP_TCD_XPCT, KQ_VP_TCD_XPMD, KQ_VP_TCD_HTK, KQ_VP_CSGCD, KQ_VP_GD_SMD, KQ_VP_GD_AGS, KQ_VP_VPK);

                    ds = GlobalServices.Service_NPCUS.UPDATE_BIEU17(p_nam, p_thang, strMa_dviqly, ma_so,TS_KH, SL_BBKT, SL_VPHC, KQ_HH_CT, KQ_HH_TUTI, KQ_HH_MD, KQ_HH_HSN, KQ_HH_SK, KQ_VP_TCD_CM, KQ_VP_TCD_XPCT, KQ_VP_TCD_XPMD, KQ_VP_TCD_HTK, KQ_VP_CSGCD, KQ_VP_GD_SMD, KQ_VP_GD_AGS, KQ_VP_VPK);
                    
                }
                Initgrid(strMa_dviqly);
                dgrNPC_BCKD05.IsReadOnly = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi : " + ex.ToString(), "Thông báo");
                strTim = "";
                return;
            }
       // MessageBox.Show(drv.Row["TS_KH"].ToString());
        }

        private void dtpThangNam_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                cmdchon.Focus();
            }
        }

        private void cboten_donvi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                dtpThangNam.Focus();
            }
        }

        private void cmdchon_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                dgrNPC_BCKD05.Focus();
            }
        }
    }
}
